package com.bancomer.batch.validators.impl;

import java.io.File;

import com.bancomer.batch.validators.GeneralValidator;
import com.bancomer.batch.pojo.FileHelper;
import com.bancomer.batch.pojo.ValidationResult;

public class ExistingFilesValidator implements GeneralValidator {

	@Override
	public ValidationResult executeValidation(FileHelper fileHelper, Object ... obj) {
		
		String validation="";
		ValidationResult result= new ValidationResult();
		
		//Una cadena vacia valida  que  exista y no este vacio: exist=true empty=true
		if(fileHelper.getFastValidation()!=null && !"".equals(fileHelper.getFastValidation()))
			validation=fileHelper.getFastValidation();	
		try {
				//Si contiene la palabra exist no validara existencia; exist=false
				if(!validation.contains("exist")){
					
					File f = new File(fileHelper.getLocalPath()+fileHelper.getName());
					
					if(f.exists()==false) throw new Exception("The file: "+fileHelper.getLocalPath()+fileHelper.getName()+" not exist!");
					
				}
				
				//Si contiene la palabra empty  no validara que este vacio empty=false
				if(!validation.contains("empty")){
					
					File f = new File(fileHelper.getLocalPath()+fileHelper.getName());
					
					if(f.exists()==false) throw new Exception("The file: "+fileHelper.getLocalPath()+fileHelper.getName()+" not exist!");
					
					if(f.length()==0) throw new Exception("The file: "+fileHelper.getLocalPath()+fileHelper.getName()+" is empty!");
					
				}	
		} catch (Exception e) {
			result.setMessage("Class: ExistingFilesValidator - "+e.getMessage());
			result.setSucces(false);
		} 
		return result;
	}

}
